package com.my.okhttps.test.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ejlchina.okhttps.HttpResult;
import com.ejlchina.okhttps.OkHttps;
import com.my.core.domain.exception.ServiceException;
import com.my.okhttps.test.service.IHealthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author timo 2021/3/15
 */
@Component
@Slf4j
public class HealthServiceImpl implements IHealthService {
    public static final String health_tar = "health_tar";
    /**连通性测试*/
    public static String TESTDOMAIN;
    @Value("${health.testdomain:https://res.newhealth.com.cn/source-token-common/dstoken/test.do}")
    public void setTESTDOMAIN(String TESTDOMAIN) {
        HealthServiceImpl.TESTDOMAIN = TESTDOMAIN;
    }
    /**获取访问令牌*/
    public static String getToken;
    @Value("${health.getToken:https://jklz.jkluzhou.cn/source-token-common/dstoken/get.do}")
    public void setGetToken(String getToken) {
        HealthServiceImpl.getToken = getToken;
    }
    /**渠道接口访问*/
    public static String channel;
    @Value("${health.channel:https://jklz.jkluzhou.cn/source-trader-standApi/api.do}")
    public void setChannel(String channel) {
        HealthServiceImpl.channel = channel;
    }
    /**appid*/
    public static String appid;
    /**appsecret*/
    public static String appsecret;
    @Value("${health.appid:}")
    public void setAppid(String appid) {
        HealthServiceImpl.appid = appid;
    }
    @Value("${health.appsecret:}")
    public void setAppsecret(String appsecret) {
        HealthServiceImpl.appsecret = appsecret;
    }

    @Override
    public String connectTest() {
        return null;
    }

    @Override
    public String getoken() {
        HttpResult httpResult = OkHttps.sync(getToken)
                .addUrlPara("a", appid)
                .addUrlPara("s", appsecret)
                .skipSerialPreproc()
                .get();
        HttpResult.Body body = httpResult.getBody();
        JSONObject jsonObject = JSON.parseObject(String.valueOf(body));
        String code = jsonObject.getString("code");
        if("0".equals(code)) {
            String access_token = jsonObject.getString("access_token");
            return access_token;
        }
        String msg = jsonObject.getString("msg");
        throw new ServiceException(msg);
    }

    @Override
    public String getAllYiyuan(String token) {
        HttpResult httpResult = OkHttps.sync(channel)
                .addUrlPara("m", "B001")
                .addUrlPara("token", token)
                .skipSerialPreproc()
                .get();
        HttpResult.Body body = httpResult.getBody();
        JSONObject jsonObject = JSON.parseObject(String.valueOf(body));
        return jsonObject.toJSONString();
    }

    @Override
    public String getkeshi(String token) {
        Map m = new HashMap();
        m.put("orgUuid", "5020");
        JSONArray b002 = operation("B002", m,token);
        return b002.toJSONString();
    }


    public JSONArray operation(String interfaceCode,Map map,String token){
        HttpResult httpResult = OkHttps.sync(channel)
                .addUrlPara("m", interfaceCode)
                .addUrlPara("token", token)
                .addBodyPara(map)
                .skipSerialPreproc()
                .tag(health_tar)
                .post();
        HttpResult.Body body = httpResult.getBody();
        JSONObject jsonObject = JSON.parseObject(String.valueOf(body));
        String code = jsonObject.getString("code");
        if("0".equals(code)) {
            JSONArray datas = jsonObject.getJSONArray("data");
            return datas;
        }
        if("0002".equals(code)){
            //未找到渠道商信息 token错误

        }
        String msg = jsonObject.getString("msg");
        throw new ServiceException(msg);
    }

}
